package com.open.capacity.utils;

import org.springframework.util.CollectionUtils;

import java.util.*;

public class CollectionUtil {


    /**
     * 获取List map的key集合
     *
     * @param listMap
     * @return
     */
    public static List<String> GetMapKey(List<Map<String, Object>> listMap) {
        if ((listMap != null) && (!listMap.isEmpty())) {
            List listKey = new ArrayList();

            Map mapResult = listMap.get(0);

            Set mapKeySet = mapResult.keySet();

            String data;

            Iterator iteratorKey = mapKeySet.iterator();
            while (iteratorKey.hasNext()) {
                data = iteratorKey.next().toString();
                listKey.add(data);
            }

            return listKey;
        }
        return null;
    }

    /**
     * List 转 String
     *
     * @param list
     * @return
     */
    public static String GetListStr(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                str.append(list.get(i));
            } else {
                str.append(",").append(list.get(i));
            }
        }
        return str.toString();
    }
}
